<?php

namespace App\Modules\Apicrzs\Services\Notify;

use App\Events\JunDaEquipmentEvent;
use App\Events\PayNotifyEvent;
use App\Library\Junda\src\CardService;
use App\Library\Wechat\PayWeChatApi;
use App\Listeners\Water\JunDaEquipmentListeners;
use App\Modules\Admin\Entities\Member\UserCardModel;
use App\Modules\Admin\Entities\Water\WaterOrderModel;
use App\Modules\Admin\Services\BaseService;
use App\Modules\Apicrzs\Entities\UsersModel;
use Illuminate\Support\Facades\Log;

class WxNotifyServices extends BaseService
{

    /**
     *  微信 支付回调
     * @param $result
     * @return string[]
     */
    public function callbackNotify($result)
    { ;
        $out_trade_no = $result['out_trade_no'];
        $transaction_id = $result['transaction_id'];
        $order_info = WaterOrderModel::getInstance()->getWaterOrderByOrderSn($out_trade_no);
//        exit(json_encode($order_info));
        xdebug($result,'处理结果-回调-api','weixin/callback');
        if (isset($result['trade_state']) && $result['trade_state'] == 'SUCCESS') {
            $eventData = [
                'attach' => $result['attach'],
                'order_sn' => $out_trade_no,
                'trade_no' => $transaction_id,
            ];

           if ($event_rest){
               return ['code' => 'SUCCESS', 'message' => '成功'];
           }else{
               return ['code' => 'error', 'message' => '失败'];
           }
            /* exit(json_encode(74));

             $openid = $result['payer']['openid'];
             $amount = $result['amount']['total']/100;
             $card_no = $order_info['card_no'];
             $user_id = $order_info['user_id'];
             $coupon_id = $order_info['coupon_id'];
             $waterOrderModel = new WaterOrderModel();

             if ($order_info['status']==1){
                 return ['code' => 'SUCCESS', 'message' => '成功'];
             }
             $update_order =[
                 'trade_no'=>$transaction_id,
                 'buy_money'=>$order_info['total_price'],
                 'pay_type'=>"weixin",
                 'pay_time'=>time(),
                 'status'=>1
             ];
             $waterOrderModel->updateWaterOrderByOrderId($order_info['order_id'],$update_order);

             if ($order_info['pay_type'] == 'weixin'){
                 if ($order_info['type']= 'consume'){
                     //消费
                     return ['code' => 'SUCCESS', 'message' => 'qq成功'];
                 }elseif ($order_info['type']= 'recharge'){
                     $card_info = UserCardModel::getInstance()->getUserByCardNo($card_no);
                     //充值
                     $update_card = [
                         'balance'=>$card_info['remaining_value'],
                         'remaining_value'=>$card_info['remaining_value'] + $order_info['total_price']
                     ];
                     UserCardModel::getInstance()->updateUserByCardId($card_info['user_card_id'],$update_card);
                     $res_jd = CardService::ICAccountCharge($card_no,$order_info['total_price'],$order_info['amount']);
                     xdebug($res_jd,'水卡充值 - 骏达 -处理结果-api','IC/callback');
                     return ['code' => 'SUCCESS', 'message' => '成功'];
                 }
             }elseif ($order_info['pay_type'] == 'alipay'){

             }*/

            return ['code' => 'SUCCESS', 'message' => '成功'];
        }else{
            return ['code' => 'error', 'message' => '失败'];
        }
    }



    public function getOrderInfo($param)
    {
        $getConfig = getWxConfig('MiniApp');
        $out_trade_no = $param['out_trade_no'];
        $params = [
            'query' => [
                // 微信订单号或者商户订单号
                'mchid' => $getConfig['mch_id']
            ]
        ];
        $order_info = WaterOrderModel::getInstance()->where(['order_sn'=>$out_trade_no])->first();

        $result = PayWeChatApi::WXPayNotify($out_trade_no);

        $order_type = $order_info['type'];
        if (isset($result['trade_state']) && $result['trade_state'] == 'SUCCESS') {
            $openid = $result['payer']['openid'];
            $amount = $result['amount']['total']/100;
            $card_no = $order_info['card_no'];
            $user_id = $order_info['user_id'];
            $coupon_id = $order_info['coupon_id'];
            $transactionModel = new WaterOrderModel();
            $transactionModel->where([ 'order_id'=>$order_info['order_id']])->update([
                'transaction_id'=>$result['transaction_id'],
                'payment_method'=>"weixin",
                'pay_type'=>"微信",
                'pay_time'=>time(),
                'status'=>1
            ]);
            $user_info = UsersModel::getInstance()->user_info($user_id);
            if($card_no){
                if($result['type']=='recharge'){
                    UserCardModel::getInstance()->where(['card_code'=>$card_no])->increment('remaining_value',$amount);
                }elseif ($result['type']=='recharge'){

                    UserCardModel::getInstance()->where(['card_code'=>$card_no])->increment('remaining_value',$amount);
                }
            }
            return true;

        } else {
            return false;
        }
    }
}
